import { _decorator, tween, Vec2 } from 'cc';
const { ccclass, property } = _decorator;
import * as fgui from "fairygui-cc";

export default class CardEntity {
    public node: fgui.GComponent = null;
    public index: number
    public info: cardInfo = null;

    public clickHandle: (info: cardInfo) => void;

    public selectedFadeHandle: () => void

    private _levelController: fgui.Controller = null;
    public selectedController: fgui.Controller = null;

    private _energyLabel: fgui.GComponent = null;

    private _x: number
    private _y: number

    constructor(node: fgui.GComponent) {
        this.node = node;

        this._levelController = this.node.getController("c1");
        this.selectedController = this.node.getController("c2");

        this.node.onClick(this.clickEvent, this);
        this._x = this.node.x;
        this._y = this.node.y;
        this._energyLabel = this.node.getChild("n9").asCom;
    }

    public updateInfo(info: cardInfo) {
        this.info = info;

        if (info.type == "RANDOM") {
            this._levelController.setSelectedIndex(4);
        }
        else if (info.shield === '0') {
            this._levelController.setSelectedIndex(0);
        }
        else {
            let index = Math.floor(Math.max(parseInt(info.shield) - 1, 1) / 3) + 1;
            index = Math.min(index,3)
            this._levelController.setSelectedIndex(index);
            this._energyLabel.text = `${info.shield}`;
        }
    }

    private clickEvent() {
        this.clickHandle(this.info);
    }

    public playFadeAnimation() {

        const duration = 0.25;
        const moveDistance = 300;
        const scale = 0.3;
        const uiOpacity = this.node._uiOpacity;

        // 计算目标位置（向下移动）
        const targetPos = new Vec2(this.node.x + this.node.width * 0.25, this.node.y + moveDistance);

        tween(this.node)
            .parallel(
                // 下移动画
                tween().to(duration, { x: targetPos.x, y: targetPos.y }, { easing: "linear" }),
                // 淡出动画（透明度从 255 到 0）
                tween().to(duration, { opacity: 0 }, {
                    onUpdate: (target: any, ratio: number) => {
                        uiOpacity.opacity = 255 * (1 - ratio); // 动态更新透明度
                    }
                }),
                tween().to(duration, { scaleX: scale, scaleY: scale }, { easing: "linear" }),
            )
            .call(() => {

            })
            .start();
    }

    public playSelectedAnimation() {
        this.info.type = ""
        this.updateInfo(this.info);
        const duration = 0.25;
        const scale = 1.3;
        const targetPos = new Vec2((this.node.parent.width - this.node.width * scale) * 0.5, (this.node.parent.height - this.node.height * scale) * 0.5);
        tween(this.node)
            .parallel(
                tween().to(duration, { x: targetPos.x, y: targetPos.y }, { easing: "linear" }),
                tween().to(duration, { scaleX: scale, scaleY: scale }, { easing: "linear" }),
            )
            .call(() => {

                this.playSelectedFadeAnimation();
            })
            .start();
    }

    private playSelectedFadeAnimation() {
        const duration = 0.25;
        const scale = 0.1;
        const targetPos = new Vec2((this.node.parent.width - this.node.width * scale) * 0.5, (this.node.parent.height - this.node.height * scale) * 0.5);
        const uiOpacity = this.node._uiOpacity;
        tween(this.node)
            .delay(1)
            .parallel(
                tween().to(duration, { x: targetPos.x, y: targetPos.y }, { easing: "linear" }),
                // 淡出动画（透明度从 255 到 0）
                tween().to(duration, { opacity: 0 }, {
                    onUpdate: (target: any, ratio: number) => {
                        uiOpacity.opacity = 255 * (1 - ratio); // 动态更新透明度
                    }
                }),
                tween().to(duration, { scaleX: scale, scaleY: scale }, { easing: "linear" }),
            )
            .call(() => {
                this.selectedFadeHandle()
            })
            .start();
    }

    public resetPosition() {
        this.node.x = this._x;
        this.node.y = this._y;
        this.node._uiOpacity.opacity = 255;
        this.node.scaleX = 1;
        this.node.scaleY = 1;
        this.selectedController.setSelectedIndex(0)
    }
}